package com.study.jdk8v2;

import com.alibaba.fastjson.JSON;
import com.study.test.extendTest.S;

import java.io.File;
import java.io.FileFilter;
import java.time.Clock;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.*;
import static org.junit.Assert.*;

/**
 * <p>添加类描述</p>
 *
 * @author chentao
 * @version v 0.1 2021/2/21 16:45
 */
public class Jdk8Test1 {

    public static void main(String[] args) {
        //Arrays.asList("a","b").forEach(e -> System.out.println(e));
        //Arrays.asList("a","b").forEach((String e) -> System.out.println(e));

   /*     String separator = ",";
        Arrays.asList( "a", "b", "d" ).forEach(
                ( String e ) -> System.out.print( e + separator ) );*/
        //testList();

        //testOptional();

        //final Clock clock = Clock.systemUTC();
        //   final LocalTime timeFromClock = LocalTime.now( clock );
        //testFile();
        //testFinal();
        // testCollect();
        //testCollect2();

        //testMaxMin();
       // testPredicate();
        //testSort();
       // testOperate();
        //threadTest();
        //testSort2();
       // functiontest();
        functiontest2();
    }
    //定义一个接口
    interface Greeting {
        void sayHello(String name);
    }



    private static void functiontest2() {
        //使用匿名内部类实现接口
        Greeting greeting = new Greeting() {
            @Override
            public void sayHello(String name) {
                System.out.println("Hello, " + name);
            }
        };
        //调用接口方法
        greeting.sayHello("Bing");
        new Greeting() {
            @Override
            public void sayHello(String name) {
                System.out.println("Hello, " + name);
            }
        }.sayHello("ssssssssssssss");


//使用Lambda表达式实现接口
        Greeting greeting2 = (name) -> System.out.println("Hello, " + name);

//调用接口方法
        greeting2.sayHello("Bing");

    }

    private static void functiontest() {
        // 1. 初始化，并且实现该接口的唯一实现方法
        Function<String,String> function = new Function<String, String>() {
            @Override
            public String apply(String str) {
                return str;
            }
        };
        System.out.println(function.apply("abc11"));

        // 使用lambda表达式
        Function<String,String> function2 = (str)->{return str;};
        //或者我们可以更简单点，把str的()括号去掉也是可以的
        // Function<String,String> function2 = str->{return str;};
        System.out.println(function2.apply("abc22"));
    }

    private static void testSort2() {
        List<Integer> integers = Arrays.asList(1, 2, 3);
        Collections.sort(integers, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
               // return o1-o2;
                return o2-o1;
            }
        });
        System.out.println(JSON.toJSON(integers));

        Collections.sort(integers,(o1, o2) -> o1-o2);
    }

    private static void threadTest() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("12312312");
            }
        }).start();

        new Thread(() -> System.out.println("lambda")).start();
    }

    private static void testOperate() {
        String[] arr = new String[]{"1"};
        //with type declaration
        MathOperation MathOperation = (int a, int b) -> a + b;
        //with out type declaration
        //with out type declaration
        MathOperation subtraction = (a, b) -> a - b;
        //with return statement along with curly braces
        MathOperation multiplication = (int a, int b) -> { return a * b; };
        //without return statement and without curly braces
        MathOperation division = (int a, int b) -> a / b;

    }

    interface MathOperation {
        int operation(int a, int b);
    }
    interface GreetingService {
        void sayMessage(String message);
    }

    private int operate(int a, int b, MathOperation mathOperation) {
        return mathOperation.operation(a, b);
    }
    private static void testSort() {
        List<String> strings = Arrays.asList("aadasd", "dasdas");
        List<String> collect = strings.stream().sorted((u1, u2) -> u1.compareTo(u2)).collect(toList());
        Collections.sort(strings,(s1,s2) -> s1.compareTo(s2));

    }

    static   Map<String, Object> hashMap = new HashMap<String, Object>();
    private static void testPredicate() {

        boolean judgeLength = judgeLength("zhangsan");
        System.out.println(judgeLength);
        boolean judgeInt = judgeInt(10);
        System.out.println(judgeInt);
        hashMap.put("zs", 18);
        hashMap.put("ls", 28);
        hashMap.put("ww", 38);
        boolean judgeKey = judgeKey("zs");
        System.out.println(judgeKey);
    }
    // 判断传入的字符串的长度是否大于5
    private static boolean judgeLength(String str) {
        return str.length() > 5 ? true : false;
    }

    // 判断传入的数字是否大于0
    private static boolean judgeInt(int i) {
        return i > 0 ? true : false;
    }

    // 判断传入的值在hash中是否存在
    private static boolean judgeKey(String key) {
        return hashMap.containsKey(key) ? true : false;
    }


    private static <T> boolean judgeByPredicate(T t, Predicate<T> predicate) {
        return predicate.test(t);
    }
    private static void test03() {
        boolean judgeByPredicate = judgeByPredicate("zhangsan", t -> t.length() > 5);
        System.out.println(judgeByPredicate);

        boolean judgeByPredicate2 = judgeByPredicate(10,t -> t > 0);
        System.out.println(judgeByPredicate2);

        hashMap.put("zs", 18);
        hashMap.put("ls", 28);
        hashMap.put("ww", 38);

        boolean judgeByPredicate3 = judgeByPredicate("zs1", t -> hashMap.containsKey(t));
        System.out.println(judgeByPredicate3);
    }

    private static void testMaxMin() {
        List<String> strings = Arrays.asList("aaaa", "b");
        String s = strings.stream().min(Comparator.comparing(e -> e.length())).get();
        System.out.println(s);
    }

    private static void testCollect2() {
        String[] words = {"Hello", "World"};
        List<String[]> collect2 = Arrays.stream(words)
                .map(word -> word.split(""))
                .distinct()
                .collect(toList());
        collect2.forEach(e -> {
            for (String s : e) {
                System.out.println(s);
            }
        });
    }

    private static void testCollect() {
        //collect(tolist()) 方法由stream里面的值生产一个列表，是一个及早求职操作
        List<String> collect = Stream.of("a", "b", "c").collect(toList());
        assertEquals(Arrays.asList("a", "b", "c"), collect);
        //map 可以将一个流中的值转换为一个新的流。一种类型转换为另外一种类型
        Stream.of("a", "b").map(string -> string.toUpperCase()).collect(toList());
        //遍历数据并检查其中的元素时，可尝试使用stream中提供的新方法filter
        List<String> collect1 = Stream.of("a", "b").filter(e -> "a".equals(e)).collect(toList());
        String[] words = {"Hello", "World"};
        List<String[]> collect2 = Arrays.stream(words)
                .map(word -> word.split(""))
                .distinct()
                .collect(toList());
        collect2.forEach(e -> System.out.println(e));

    }

    private static void testFinal() {
        String name = getname();
        name = formatUserName(name);


    }

    private static String formatUserName(String name) {
        name = name + "sadsd";
        return null;
    }

    private static String getname() {
        return "aa";
    }

    private static void testFile() {
        File[] hiddenFiles = new File(".").listFiles(new FileFilter() {
            public boolean accept(File file) {
                // 筛选隐藏文件
                return file.isHidden();
            }
        });
        new File(".").listFiles(File::isHidden);
        new File(".").listFiles(File -> File.isHidden());
    }

    private static void testList() {
        Arrays.asList("c", "d").sort((o1, o2) -> {
            int i = o1.compareTo(o2);
            return i;
        });
    }

    private static void testOptional() {
        Optional<String> firstName = Optional.of("tom");
        System.out.println("1" + firstName.isPresent());
        System.out.println("2" + firstName.orElseGet(() -> "ssssss"));
    }
}
